package com.xiaoke.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xiaoke.domain.entity.Tag;
import com.xiaoke.domain.entity.TagMiddle;
import com.xiaoke.domain.vo.TagVo;
import com.xiaoke.enums.TagTargetEnum;
import com.xiaoke.mapper.TagMiddleMapper;
import com.xiaoke.service.TagMiddleService;
import com.xiaoke.service.TagService;
import com.xiaoke.utils.BeanCopyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

import static com.xiaoke.constants.SystemConstants.STATUS_NORMAL;

/**
 * 标签表中间表(TagMiddle)表服务实现类
 *
 * @author xiaoke
 * @since 2023-03-17 10:38:56
 */
@Service("tagMiddleService")
public class TagMiddleServiceImpl extends ServiceImpl<TagMiddleMapper, TagMiddle> implements TagMiddleService {

    @Lazy
    @Resource
    TagService tagService;

    @Override
    public List<TagVo> getByIdTagList(Long id, TagTargetEnum targetEnum) {
        LambdaQueryWrapper<TagMiddle> middleLambdaQueryWrapper = new LambdaQueryWrapper<>();

        middleLambdaQueryWrapper
                .eq(TagMiddle::getTargetCategory,targetEnum.getCode())
                .eq(TagMiddle::getTargetId,id);

        List<TagMiddle> list = list(middleLambdaQueryWrapper);

        List<Tag> tagList = new ArrayList<>();
        list.stream()
                .peek(tagMiddle-> {
                    LambdaQueryWrapper<Tag> tagLambdaQueryWrapper = new LambdaQueryWrapper<>();
                    tagLambdaQueryWrapper.eq(Tag::getId,tagMiddle.getLabelId())
                            .eq(Tag::getStatus,STATUS_NORMAL);
                    tagList.add(tagService.getOne(tagLambdaQueryWrapper));
                })
                .collect(Collectors.toList());

        return BeanCopyUtils.copyBeanList(tagList, TagVo.class);
    }

    @Override
    public Set<Long> getByIdsTargetIds(Long id, TagTargetEnum targetEnum) {
        Set<Long> targetIds = new HashSet<>();

        LambdaQueryWrapper<TagMiddle> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper
                .eq(TagMiddle::getLabelId,id)
                .eq(TagMiddle::getTargetCategory,targetEnum.getCode());

        List<TagMiddle> list = list(queryWrapper);

        list.stream()
                .peek(tagMiddle ->  targetIds.add(tagMiddle.getTargetId()))
                .collect(Collectors.toList());

        return  targetIds;
    }

    @Override
    public Set<Long> getByIdsListTargetIds(Set id, TagTargetEnum targetEnum) {
        Set<Long> targetIds = new HashSet<>();
        LambdaQueryWrapper<TagMiddle> queryWrapper = new LambdaQueryWrapper<>();

        queryWrapper
                .in(TagMiddle::getLabelId,id)
                .eq(TagMiddle::getTargetCategory,targetEnum.getCode());

        List<TagMiddle> list = list(queryWrapper);

        list.stream()
                .peek(tagMiddle ->  targetIds.add(tagMiddle.getTargetId()))
                .collect(Collectors.toList());
        return targetIds;
    }
}

